package com.example.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Account;
import com.example.entity.ShareFile;
import com.example.entity.TransferStation;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.ShareFileMapper;
import com.example.mapper.TransferStationMapper;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.RoundingMode;
import java.util.List;

/**
 * 分享文件信息业务处理
 **/
@Service
public class ShareFileService {

    @Resource
    private ShareFileMapper shareFileMapper;

    @Resource
    private TransferStationMapper transferStationMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 新增
     */
    public void add(ShareFile shareFile) {
        Account currentUser = TokenUtils.getCurrentUser();
        shareFile.setUserId(currentUser.getId());
        TransferStation transferStation = transferStationMapper.selectByUserId(shareFile.getReceiverId());
        if (ObjectUtil.isEmpty(transferStation)) {
            throw new CustomException("500", "对方暂无中转站信息");
        } else {
            if (transferStation.getCurrentSize().add(shareFile.getCurrentSize()).compareTo(transferStation.getMaxSize()) > 0) {
                throw new CustomException("500", "对方中转站内存不足");
            } else {
//                插入分享文件
                shareFile.setCreateTime(DateUtil.now());
                shareFile.setIsRead("否");
                shareFile.setTransferStationId(transferStation.getId());
                shareFileMapper.insert(shareFile);
//                更新中转站存储
                transferStation.setCurrentSize(transferStation.getCurrentSize().add(shareFile.getCurrentSize()));
                transferStationMapper.updateById(transferStation);
            }
        }

    }

    /**
     * 删除
     */
    @Transactional
    public void deleteById(Integer id) {
//        更新对应的中转站
        ShareFile shareFile = shareFileMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(shareFile)) {
            TransferStation transferStation = transferStationMapper.selectById(shareFile.getTransferStationId());
            if (ObjectUtil.isNotEmpty(transferStation)) {
                transferStation.setCurrentSize(transferStation.getCurrentSize().divide(shareFile.getCurrentSize(),10, RoundingMode.HALF_UP));
                transferStationMapper.updateById(transferStation);
            }
        }
        shareFileMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            shareFileMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(ShareFile shareFile) {
        shareFileMapper.updateById(shareFile);
    }

    /**
     * 根据ID查询
     */
    public ShareFile selectById(Integer id) {
        return shareFileMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<ShareFile> selectAll(ShareFile shareFile) {
        List<ShareFile> list = shareFileMapper.selectAll(shareFile);
        for (ShareFile shareFile1 : list) {
            User user = userMapper.selectById(shareFile1.getReceiverId());
            if (ObjectUtil.isNotEmpty(user)) {
                shareFile1.setReceiverName(user.getName());
            }
            TransferStation transferStation = transferStationMapper.selectById(shareFile1.getTransferStationId());
            if (ObjectUtil.isNotEmpty(transferStation)) {
                shareFile1.setTransferStationName(transferStation.getName());
            }
        }
        return list;
    }

    /**
     * 分页查询
     */
    public PageInfo<ShareFile> selectPage(ShareFile shareFile, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ShareFile> list = this.selectAll(shareFile);
        return PageInfo.of(list);
    }

    public void getFileRead() {
        Account currentUser = TokenUtils.getCurrentUser();
        ShareFile shareFile = new ShareFile();
        shareFile.setReceiverId(currentUser.getId());
        List<ShareFile> list = shareFileMapper.selectAll(shareFile);
        for (ShareFile shareFile1 : list) {
            shareFile1.setIsRead("是");
            shareFileMapper.updateById(shareFile1);
        }
    }

    public Integer isReadFile() {
        Account currentUser = TokenUtils.getCurrentUser();
        ShareFile shareFile = new ShareFile();
        shareFile.setIsRead("否");
        shareFile.setReceiverId(currentUser.getId());
        List<ShareFile> list = shareFileMapper.selectAll(shareFile);
        return list.size();
    }
}